#include "../inc/recursive_reverse.hpp"
#include <iostream>
using namespace std;
Node *recursive_reverse(Node *p)
{
    //如果链表为空或者为单节点，直接返回
    if (p == nullptr || p->next == nullptr)
    {
        return p;
    }
    //否则继续递归处理下一个节点
    auto nextnode = recursive_reverse(p->next);
    //改变p的下一个节点next指向，链表逆转
    p->next->next = p;
    //改变p的next指向
    p->next = nullptr;
    return nextnode;
}

void printList(Node *p)
{
    while (p != nullptr)
    {
        cout << p->data << " -> ";
        p = p->next;
    }

    cout << " nullptr " << endl;
}

Node *createList()
{
    auto node1 = new Node(1);
    auto node2 = new Node(2);
    auto node3 = new Node(3);
    node1->next = node2;
    node2->next = node3;
    return node1;
}

void delocateList(Node *p)
{
    while (p != nullptr)
    {
        auto temp = p;
        p = p->next;
        delete temp;
    }
}